package com.example.fuliniu.customviews;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.graphics.Shader;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.Calendar;

/**
 * Created by fuli.niu on 2016/7/6.
 */
public class PedometerView extends View {

    private Context context;
    private Paint headerPaint;// 头部画笔
    private Rect headerPaintRect;// 用于绘制头部文字区域，如：“步数”
    private int headerPaintHeight;// 头部的高度
    private Paint bottomPaint;// 底部日期画笔
    private Rect bottomPaintRect;// 底部日期文字 区域
    private int bottomPaintHeight;// 底部日期的高度
    private Paint brokenLinePaint;// 折线图画笔
    private Paint stepsNumberPaint;// 步数弹出文字画笔
    private float stepsNumberX;// 步数弹出文字x坐标
    private float stepsNumberY;// 步数弹出文字y坐标
//    private String stepsNubmer; // 步数弹出文字的值
    private String todayStepsNubmer; // 今天的步数
    private float[] positionXs;// 用于记住每个日期的x坐标
    private float[] positionYs;// 和positionXs的个数一直步数Y坐标
    private int clickPosition;// 哪个日期被点击了
    private int pedometerViewWidth;// 画布宽度
    private int pedometerViewHeight;// 画布高度
    private float headerTextSize;// 头部字体大小
    private float bottomTextSize;// 底部日期字体大小

    public PedometerView(Context context) {
        this(context, null);
    }

    public PedometerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PedometerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
//        stepsNubmer = "9999";// 默认9999
        positionXs = new float[7];
        positionYs = new float[]{1389, 10323, 2422, 5343, 142, 8012, 9011};
        clickPosition = -1;
        headerTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 26,
                getResources().getDisplayMetrics());
        bottomTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12,
                getResources().getDisplayMetrics());
        todayStepsNubmer = (int) positionYs[6] + "" ;
        // 先准备画笔，得到view的高度
        drawHeader();
        drawBottom();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawHeader(canvas);
        drawBottom(canvas, clickPosition);
        drawBrokenLineGraph(canvas);
        drawStepsNumber(canvas , clickPosition > -1 ? positionYs[clickPosition] + "" : "9999");
//        drawStepsNumber(canvas, stepsNubmer);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        pedometerViewWidth = getMeasuredWidth();
        pedometerViewHeight = getMeasuredHeight();
        setMeasuredDimension(pedometerViewWidth, bottomPaintHeight + 30);
    }

    /**
     * 绘制折线图头部，且字体大小固定
     */
    private void drawHeader() {
        headerPaint = new Paint();
        headerPaint.setStyle(Paint.Style.STROKE);
        headerPaint.setAntiAlias(true); //去锯齿
        headerPaint.setTextSize(headerTextSize);
        headerPaintRect = new Rect();
        headerPaint.getTextBounds(todayStepsNubmer, 0, todayStepsNubmer.length(), headerPaintRect);
        headerPaintHeight = 30 * 2 + headerPaintRect.height();
    }

    private void drawHeader(Canvas canvas) {
        headerPaint.setTextSize(headerTextSize);
        headerPaint.setColor(Color.WHITE);
        canvas.drawText("步数", 40, headerPaintRect.height() + 30, headerPaint);
        canvas.drawText(todayStepsNubmer, pedometerViewWidth - headerPaintRect.width() - 40, headerPaintRect.height() + 30, headerPaint);
        headerPaintHeight = 30 * 2 + headerPaintRect.height();
        canvas.drawLine(40, headerPaintHeight, pedometerViewWidth - 40, headerPaintHeight, headerPaint);

        headerPaint.setTextSize(bottomTextSize);
        headerPaint.setColor(Color.parseColor("#55D5D8"));
        Rect rect = new Rect() ;
        headerPaint.getTextBounds("1w" , 0 , 2 , rect);

        Path path = new Path();
        path.moveTo(40 , headerPaintHeight + 140 + 30 );
        path.lineTo(pedometerViewWidth - 40 - rect.width() , headerPaintHeight + 140 + 30);
        path.close();
        PathEffect pathEffect = new DashPathEffect(new float[]{5,5,5,5} , 1) ;
        headerPaint.setPathEffect(pathEffect) ;
        canvas.drawPath(path , headerPaint);

        canvas.drawText("1w" , pedometerViewWidth - 40 -40 , headerPaintHeight + 140 + 30 + rect.height() / 2 , headerPaint);
//        canvas.drawLine(40, headerPaintHeight + 140 + 30, pedometerViewWidth - 40, headerPaintHeight + 140 + 30, headerPaint);
        canvas.drawLine(40, headerPaintHeight + 300 + 30, pedometerViewWidth - 40, headerPaintHeight + 300 + 30, headerPaint);
    }

    /**
     * 绘制底部日期
     */
    private void drawBottom() {
        bottomPaint = new Paint();
        bottomPaint.setStyle(Paint.Style.FILL);
        bottomPaint.setAntiAlias(true); //去锯齿
        bottomPaint.setTextSize(bottomTextSize);
        bottomPaintRect = new Rect();
        bottomPaint.getTextBounds("月", 0, 1, bottomPaintRect);
        bottomPaintHeight = bottomPaintRect.height() + headerPaintHeight + 300 + 30 + 30;
    }

    /**
     * 绘制底部日期
     *
     * @param canvas
     * @param clickPosition 根据位置更改日期的颜色，默认为-1（在onDraw（）中），不需要更改日期的颜色，
     */
    private void drawBottom(Canvas canvas, int clickPosition) {
        for (int i = 0; i < 7; i++) {
            bottomPaint.setColor(clickPosition == i ? Color.WHITE : Color.parseColor("#55D5D8"));
            positionXs[i] = pedometerViewWidth / 14 * (1 + 2 * i);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis() - 6 * 24 * 60 * 60 * 1000);
            int maxDayOfMonth = calendar.getMaximum(Calendar.DAY_OF_MONTH);
            String text;
            if (i == 0) {
                text = calendar.get(Calendar.MONTH) + 1 + "月" + calendar.get(Calendar.DAY_OF_MONTH);
            } else {
                text = (i + calendar.get(Calendar.DAY_OF_MONTH)) % maxDayOfMonth + "";
            }
            canvas.drawText(text, positionXs[i] - text.length() * bottomPaintRect.width() / 2, bottomPaintHeight, bottomPaint);
        }
    }

    /**
     * 绘制折线图
     *
     * @param canvas
     */
    private void drawBrokenLineGraph(Canvas canvas) {
        brokenLinePaint = new Paint();
        brokenLinePaint.setStyle(Paint.Style.FILL);
        brokenLinePaint.setAntiAlias(true);
        brokenLinePaint.setColor(Color.WHITE);
        brokenLinePaint.setStrokeWidth(2);
        for (int i = 0; i < 6; i++) {
            float start = headerPaintHeight + 300 + 30 - positionYs[i] * 160 / 10000 ;
            float end = headerPaintHeight + 300 + 30 - positionYs[i + 1] * 160 / 10000 ;
            canvas.drawLine(positionXs[i], start , positionXs[i + 1], end , brokenLinePaint);
            canvas.drawCircle(positionXs[i], start , 5, brokenLinePaint);
//            path = new Path() ;
//            path.moveTo(positionXs[i] , positionYs[i]) ;
//            path.lineTo(positionXs[i + 1] , positionYs[i + 1]);
//            path.lineTo(positionXs[i + 1] , bottomPaintHeight + 30);
//            path.lineTo(positionXs[i] , bottomPaintHeight + 30);
//            path.close();
//            float maxH = positionYs[i] > positionYs[i + 1] ? positionYs[i] : positionYs[i + 1] ;
//            Shader shader = new LinearGradient(positionXs[i] , maxH , positionXs[i + 1] , bottomPaintHeight + 30 , new int[]{Color.parseColor("#ddffffff") , Color.TRANSPARENT} , null , Shader.TileMode.CLAMP) ;
//            brokenLinePaint.setShader(shader) ;
//            canvas.drawPath(path , brokenLinePaint);

        }
        // 最后一个点变大
        canvas.drawCircle(positionXs[6], headerPaintHeight + 300 + 30 - positionYs[6] * 160 / 10000 , 10, brokenLinePaint);
    }

    /**
     * 绘制弹出步数，默认在屏幕外
     *
     * @param canvas
     */
    private void drawStepsNumber(Canvas canvas, String stepsNumber) {
        if (clickPosition == -1) {
            stepsNumberX = -100;
            stepsNumberY = -100;
        }
        stepsNumberPaint = new Paint();
        stepsNumberPaint.setStyle(Paint.Style.FILL);
        stepsNumberPaint.setAntiAlias(true); //去锯齿
        stepsNumberPaint.setTextSize(bottomTextSize);
        stepsNumberPaint.setColor(Color.parseColor("#55D5D8"));
        Rect stepsNumberPaintRect = new Rect();
        stepsNumberPaint.getTextBounds(stepsNumber, 0, stepsNumber.length(), stepsNumberPaintRect);
        canvas.drawText(stepsNumber, (int) (stepsNumberX - stepsNumberPaintRect.width() / 2) , stepsNumberY, stepsNumberPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /**
         * 确定哪一天的步数被点击了
         */
        for (int i = 6; i > -1; i--) {
            if (positionXs[i] - pedometerViewWidth / 14 < event.getX()) {
                clickPosition = i;
                break;
            }
        }
//        Toast.makeText(context , event.getX() + " | " + event.getY() + " | " + clickPosition  ,Toast.LENGTH_SHORT).show();
        /**
         * 修改弹出步数文字的绘制位置和大小
         */
        stepsNumberX = positionXs[clickPosition];
        stepsNumberY = headerPaintHeight + 300 + 30 - positionYs[clickPosition] * 160 / 10000;
//        stepsNubmer = 9999 - clickPosition * 100 + "";
        startValueAnimator();
        return super.onTouchEvent(event);
    }

    /**
     * 开启绘制上升步数动画
     */
    private void startValueAnimator() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 2);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                stepsNumberY = stepsNumberY > headerPaintHeight + 30 + 10 ? stepsNumberY -= 20 : headerPaintHeight + 30 + 10 ;
                invalidateView();
            }
        });
        valueAnimator.setDuration(400);
        valueAnimator.start();
    }

    /**
     * 重绘
     */
    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }

}
